<?php
/**
 * Created by PhpStorm.
 * User: Jessewang
 * Date: 2017/3/26
 * Time: 下午9:48
 */

namespace App\Service;
use App\DAO\User;
use App\DAO\UserInfoDAO;
use App\DAO\UserLoginDAO;
use App\Libs\QqApi;
use App\Libs\WeiBoApi;
use Swoole;
use App\Libs\WeChatApi;
use App\DAO;
use App\Libs\TimApi;


class UserService extends BaseService
{
    public $type ; //0:qq 1:wechat 2:weibo
    public $snsapi;
    public $timapi;
    public $userLogin;
    public $userInfo;
    function __construct($plat=null)
    {
        if($plat!=null)
            $this->setPlat($plat);

//        if($this->type == 1)
//        {
//            $this->snsapi = new WeChatApi();
//        }
//        else if($this->type == 0)
//        {
//            $this->snsapi = new QqApi();
//        }

        $this->timapi = new TimApi();
        $this->userInfo= new UserInfoDAO();
        $this->userLogin = new UserLoginDAO();
    }
    function setPlat($plat)
    {
        if($plat == 1 )
            $this->snsapi = new WeChatApi();
        else if ($plat == 0)
            $this->snsapi = new QqApi();
        else if ($plat == 2)
            $this->snsapi = new WeiBoApi();
        $this->type = $plat;
    }

    function updateUserInfo($userid,$token,$data)
    {
        $this->userInfo->set($userid,$data,'user_id');
        $this->userLogin->setUserLoginToCacheByUid($userid,$data);
        $this->userLogin->setUserLoginToCacheByToken($token,$data);
        return $this->userLogin->getUserLoginFromCacheByToken($token);
    }

    function auth($code)
    {

        return $this->snsapi->getAccessToken($code);

    }

    function getUserInfo($accesstoken,$openid)
    {
        $userinfo = null;
        $ret = $this->snsapi->getUserInfo($accesstoken,$openid);
        //var_dump($ret);
        if($this->type == 0 && $ret!=null)
        {
            if($ret->ret == 0 ) {
                $userinfo['user_name'] = $ret->nickname;
                $userinfo['user_sex'] = $ret->gender == '男' ? 1 : 0;
                $userinfo['user_head_img'] = $ret->figureurl_qq_2;
            }

        }
        if($this->type == 2 )
        {
            {
                $userinfo['user_name'] = $ret->name;
                $userinfo['user_sex'] = $ret->gender == 'm' ? 1 : 0;
                $userinfo['user_head_img'] = $ret->profile_image_url;
            }

        }
        return $userinfo;
    }
    function getUserLoginByUserid($userid)
    {
        $ret = $this->userLogin->getUserLoginFromCacheByUid($userid);
        //var_dump($ret);
        if(!$ret) {
            $ret = $this->userLogin->get($userid, 'id');
        }
        return $ret;
    }
    function getUserLoginByToken($token)
    {
        $ret = $this->userLogin->getUserLoginFromCacheByToken($token);
       // var_dump($ret);
        if(!$ret) {
            $ret = $this->userLogin->get($token, 'token');
        }
        return $ret;
    }
    function getUserInfobyUserId($userid)
    {
        $key = 'userinfo#'.$userid;
        $cachuserinfo = \Swoole::getInstance()->redis->hGetAll($key);
        if($cachuserinfo)
        {
            return $cachuserinfo;
        }
        else {
            $userDao = new UserInfoDAO();
            $ret = $this->userInfo->get($userid, 'user_id');
            return $ret;
        }
    }
    function genToken($openid)
    {
        return md5($openid.date('Y-m-d', time()));
    }
    function getUserInfoByToken($token)
    {
       // $userLogin  = new UserLoginDAO();
        $userlogin = $this->userLogin->get($token,'token');
        $userid = $userlogin['id'];
        $userinfo  =  $this->userInfo->get($userid,'user_id');

    }
    function  loginwithcode($code)
    {
        $ret = $this->snsapi->getAccessToken($code);
        $ret = json_decode($ret);
        $openid = $ret['openid'];
        $accesstoken = $ret['accessstoken'];
        $userLogin = new UserLoginDAO();
        $userloginInfo =  $userLogin->get($openid,'open_id');


        $userinfoDao = new UserInfoDAO();
        if($userloginInfo)
        {
            $data = array(
                'access_token'=>$accesstoken
            );
            $userloginInfo->set($openid,$data,'open_id');
            $userid = $userloginInfo['id'];
            $userinfo = $this->getUserLoginByUserid($userid);
            if($userinfo['user_age'] == null)
            {
                $ret = array(
                    "errorcode" =>'0',
                    'errmsg'=>"需要设置年龄",
                    'userinfo'=>$userinfo
                );
            }
        }
        else
        {
                $userloginIinfo= array(
                    'open_id' => $openid,
                    'access_token' => $accesstoken,
                );

                $userid = $userLogin->insert($userloginIinfo);
               // var_dump($userid);
                $user3rdinfo = $this->getUserInfo($accesstoken,$openid);
                $userinfo['user_name'] = $user3rdinfo['user_name'];
                $userinfo['user_nick'] = $user3rdinfo['user_nick'];
                $userinfo['user_sex'] = $user3rdinfo['user_sex'];
                $userinfo['user_head_img'] = $user3rdinfo['user_head_img'];
                $userinfo['user_id']  = $userid;
                $userinfoDao->insert($userinfo);
                $userloginextInfo['token']  = $this->genToken($openid);
                $usersig =  $this->timapi->genSig($userid);
                $userloginextInfo['user_sig'] = $usersig;
                $userLogin->set($openid,$userloginextInfo,'open_id');
                $userinfo = array_merge($userinfo,$userloginextInfo,$userloginIinfo);
                $this->timapi->reg_account($userinfo);
                $this->timapi->setPorfile($userid,$userinfo);
                $this->userLogin->setUserLoginToCache($userloginextInfo['token'],$userinfo);
                $this->userLogin->setUserLoginToCacheByUid($userid,$userinfo);
        }
        $ret = array(
            "errorcode" =>'0',
            'errmsg'=>"登录成功",
            'status'=> '0',
            'userinfo'=>$userinfo
        );

        return $ret;


    }
    function  loginWithToken($userid,$token)
    {
        $userLogin = new UserLoginDAO();
        $userLogininfo = $userLogin->get($token,'token');
        if($userLogininfo && $userLogininfo['id'] == $userid)
        {
            $userinfo = $this->userLogin->getUserLoginFromCacheByToken($userid);
            if($userinfo['user_age'] = null)
            {
                    $ret = array(
                        "errorcode" =>'0',
                        'errmsg'=>"需要设置年龄",
                        'status'=>'1',
                        'userinfo'=>$userinfo
                    );

            }
            else
            {

                   $ret = array(
                       "errorcode" =>'0',
                       'errmsg'=>"登录成功",
                       'status'=> '0',
                    'userinfo'=>$userinfo
                );
                return $ret;

            }

        }
    }
    function loginWithAccessToken($openid,$accesstoken)
    {


//        if($accesstoken != NULL&&$openid != null)
//        {
//            $auth = $this->snspai->auth($openid,$accesstoken);
//        }
        $auth = true;
        if($auth)
        {

            $userloginInfo =  $this->userLogin->get($openid,'open_id');
            //var_dump($userloginInfo);
            if(!empty($userloginInfo))
            {
              // var_dump(1111);
                $data = array(
                    'access_token'=>$accesstoken
                );
                $this->userLogin->set($openid,$data,'open_id');
                $userid = $userloginInfo['id'];

                $userinfo = $this->getUserLoginByUserid($userid);
              //  $userinfo['user_age'] = 0;
                //var_dump($userinfo);
                if($userinfo['user_age'] == 0)
                {
                    $ret = array(
                        "errorcode" =>'0',
                        'errmsg'=>"需要设置年龄",
                        'status'=>'1',
                        'userinfo'=>$userinfo

                    );
                    return $ret;
                }

            }
            else
            {
                $userloginIinfo= array(
                    'open_id' => $openid,
                    'access_token' => $accesstoken,
                    'reg_src' =>$_GET['plat']
                );
                $user3rdinfo = $this->getUserInfo($accesstoken,$openid);
                if(empty($user3rdinfo))
                {
                    $ret = array(
                        "errorcode" =>'0',
                        'errmsg'=>"登录失败，重新登录",
                        'status'=>'1',
                        'userinfo'=>''
                    );
                    return $ret;
                }
                $userid = $this->userLogin->insert($userloginIinfo);

//                $user3rdinfo = array(
//                    'user_name'=>'jessewang',
//                    'user_nick'=>'jessewang',
//                    'user_sex'=>1,
//                    'user_head_img'=>"www.qq.com"
//                );
                $userinfo['user_name'] = $user3rdinfo['user_name'];
                $userinfo['user_nick'] = $user3rdinfo['user_name'];
                $userinfo['user_sex'] = $user3rdinfo['user_sex'];
                $userinfo['user_head_img'] = $user3rdinfo['user_head_img'];
                $userinfo['user_id']  = $userid;

                $this->userInfo->insert($userinfo);
                $userloginextInfo['token']  = $this->genToken($openid);
                $usersig =  $this->timapi->genSig($userid);

                $userloginextInfo['user_identifier'] = $userid;
                $userloginextInfo['user_sig'] = $usersig;
                $this->userLogin->set($openid,$userloginextInfo,'open_id');
                $userinfo = array_merge($userinfo,$userloginextInfo,$userloginIinfo);
                $this->timapi->reg_account($userinfo);
                $this->timapi->setPorfile($userid,$userinfo);
                $this->userLogin->setUserLoginToCacheByToken($userloginextInfo['token'],$userinfo);
                $this->userLogin->setUserLoginToCacheByUid($userid,$userinfo);

            }
            if($userinfo['user_age'] == null)
            {
                $ret = array(
                    "errorcode" =>'0',
                    'errmsg'=>"需要设置年龄",
                    'status'=>'1',
                    'userinfo'=>$userinfo
                );
                return $ret;
            }
            $ret = array(
                "errorcode" =>'0',
                'errmsg'=>"登录成功",
                'status'=> '0',
                'userinfo'=>$userinfo
            );


        }
        else
        {
            $ret = array(
                "errorcode" =>'1',
                'errmsg'=>"登录成功",
                'status'=> '1',
                'userinfo'=>''
            );
        }
        return $ret;
    }




}